package com.shengqi.xinnongren.work.ui.user.activities;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.CountDownTimerUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.common.MyWebViewActivity;
import com.shengqi.xinnongren.work.dialog.VerifyCaptchaDialog;
import com.shengqi.xinnongren.work.ui.user.model.CaptchaBean;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.user.model.ProtocolBean;
import com.shengqi.xinnongren.work.ui.user.presenter.RegisterPresenter;
import com.shengqi.xinnongren.work.ui.user.view.RegisterView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by LuoYaWei on 2018/6/14.
 */
public class RegisterActivity extends BaseMvpActivity<RegisterView, RegisterPresenter> implements RegisterView {

    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.edt_captcha)
    EditText edt_captcha;
    @BindView(R.id.tv_captcha)
    TextView tv_captcha;
    @BindView(R.id.edt_authCode)
    EditText edt_authCode;
    @BindView(R.id.edt_pwd)
    EditText edt_pwd;
    @BindView(R.id.edt_storeName)
    EditText edt_storeName;
    @BindView(R.id.iv_show_pwd)
    ImageView iv_showPwd;
    @BindView(R.id.sw_agree)
    Switch sw_agree;

    private VerifyCaptchaDialog dialog;

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tv_captcha)
    void showCaptchaDialog() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone) || strPhone.length() < 11) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }
        mPresenter.getCaptchaUrl(strPhone);
    }

    @OnClick(R.id.iv_show_pwd)
    public void onShowPwdClicked() {
        iv_showPwd.setSelected(!iv_showPwd.isSelected());
        if (iv_showPwd.isSelected()) {
            edt_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            edt_pwd.setSelection(edt_pwd.getText().toString().length());
        } else {
            edt_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            edt_pwd.setSelection(edt_pwd.getText().toString().length());
        }
    }

    @OnClick(R.id.tv_login)
    void gotoLogin() {
        finish();
    }

    @OnClick(R.id.tv_protocol)
    void showProtocol() {
        mPresenter.getProtocol();
    }

    @OnClick(R.id.btn_register)
    void register() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }

        String strCaptcha = edt_captcha.getText().toString();
        if (TextUtils.isEmpty(strCaptcha)) {
            ToastHelper.getInstance(this).showWarn("请输入验证码");
            return;
        }
        String strAuthCode = edt_authCode.getText().toString();
        if (TextUtils.isEmpty(strAuthCode)) {
            ToastHelper.getInstance(this).showWarn("请输入授权码");
            return;
        }

        String strPwd = edt_pwd.getText().toString();
        if (TextUtils.isEmpty(strPwd)) {
            ToastHelper.getInstance(this).showWarn("请输入密码");
            return;
        }
        if (!sw_agree.isChecked()) {
            ToastHelper.getInstance(this).showWarn("请同意用户协议");
            return;
        }
        if (strPwd.length() < 6) {
            ToastHelper.getInstance(this).showWarn("密码最少6位");
            return;
        }
        //开始注册
        mPresenter.register(strPhone, strPwd, strCaptcha, strAuthCode,edt_storeName.getText().toString());
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }

    @Override
    public void showCaptchaUrl(HttpRespond<CaptchaBean> respond) {
        CaptchaBean bean = respond.data;
        LogUtil.e(bean.captchaUrl);
        dialog = VerifyCaptchaDialog.newInstance(
                edt_phone.getText().toString(), bean.captchaUrl, captcha -> mPresenter.sendSMS(edt_phone.getText().toString(), captcha));
        dialog.show(getSupportFragmentManager(), "captcha");
    }

    @Override
    public void sendSMSRes(HttpRespond respond) {
        ToastHelper.getInstance(this).showSucceed("短信发送成功");
        if (dialog != null) {
            dialog.dismiss();
        }
        CountDownTimerUtils countDownTimerUtils =
                new CountDownTimerUtils(tv_captcha, 60000, 1000);
        countDownTimerUtils.start();

    }

    @Override
    public void registerRes(HttpRespond respond) {
        ToastHelper.getInstance(this).showSucceed("注册成功");
        finish();

    }

    @Override
    public void showProtocol(HttpRespond<ProtocolBean> response) {
        startActivity(MyWebViewActivity.getIntent(this, "用户协议", response.data.getContent()));
    }
}
